﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;

using OA.Model;
using OA.Web.Areas.Member.Controllers;
using OA.Web.Attribute;
using OA.Web.Code;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Web;
using X.PagedList;

namespace OA.Web.Areas.Management.Controllers
{
    [Area("Management")]
    public class OfficialDocumentController : BaseMemberController
    {

        private IMemoryCache _cache;
        public OfficialDocumentController(ICookie cookie, IHttpContextAccessor accessor, IMemoryCache cache) : base(accessor, cookie, cache)
        {
            _cache = cache;
        }

        [MyActionFilter("206006", "红头文件", "管理中心", true, "iconhongtou")]
        public IActionResult Index()
        {
            return View();
        }

        [MyActionFilter("206006", "红头文件", "管理中心", true, "icongonggao1")]
        public IActionResult GetData(string keyword, int page = 1, int limit = 15)
        {
            ViewBag.Keyword = keyword;
            Expression<Func<Notice, bool>> where = m => m.Type == "红头文件";
            if (!string.IsNullOrWhiteSpace(keyword))
            {
                where = m => m.Type == "红头文件" && (m.Title.Contains(keyword));
            }
            var list = this.dbContextFactory.DbContext.Notice.Where(where).Select(SimpleNotice.Map).OrderByDescending(m => m.ID).ToPagedList(page, limit);
            var result = new
            {
                code = "0",
                msg = "",
                count = list.TotalItemCount,
                data = list
            };
            return Json(result);
        }

        [MyActionFilter("206006001", "添加红头文件", "红头文件", false, "")]
        [HttpGet]
        public IActionResult Create()
        {
            return View(new Notice() { SendTime = DateTime.Now });
        }

        [MyActionFilter("206006001", "添加红头文件", "红头文件", false, "")]
        [HttpPost]
        public IActionResult Create(Notice model)
        {
            BaseResult baseResult = new BaseResult();
            model.Type = "红头文件";
            if (!ModelState.IsValid)
            {
                baseResult.State = 400;
                baseResult.Msg = this.GetModelStateError(ModelState);
                return Json(baseResult);
            }
            else
            {
                this.AddInfo<Notice>(model);
                baseResult.Msg = "红头文件添加成功";
                return Json(baseResult);


            }

        }
        [MyActionFilter("206006002", "编辑红头文件", "红头文件", false, "")]
        [HttpGet]
        public IActionResult Edit(int id)
        {
            Notice infoNotice = this.GetInfo<Notice>(id);
            if (infoNotice == null)
            {
                return Redirect("/Admin/Home/Info?msg=" + System.Web.HttpUtility.UrlEncode("沒有找到该红头文件", Encoding.UTF8));
            }
            return View(infoNotice);
        }

        [MyActionFilter("206006002", "编辑红头文件", "红头文件", false, "")]
        [HttpPost]
        public IActionResult Edit(int id, Notice model)
        {
            model.Type = "红头文件";
            BaseResult baseResult = new BaseResult();
            if (!ModelState.IsValid)
            {
                baseResult.State = 400;
                baseResult.Msg = this.GetModelStateError(ModelState);
                return Json(baseResult);
            }
            Notice infoNotice = this.GetInfo<Notice>(id);
            if (infoNotice == null || infoNotice.Type != "红头文件")
            {
                baseResult.State = 400;
                baseResult.Msg = "沒有找到该红头文件";
                return Json(baseResult);
            }
            this.Update<Notice>(infoNotice, model, null, new string[] { "ID" });
            baseResult.Msg = "红头文件修改成功";
            return Json(baseResult);

        }


        [MyActionFilter("206006003", "删除红头文件", "红头文件", false, "")]
        [HttpGet]
        public IActionResult Delete(int id)
        {
            Expression<Func<Notice, bool>> where = m => m.ID == id&&m.Type== "红头文件";
            this.Delete<Notice>(where);
            return Json(new { State = 200, Msg = "红头文件删除成功" });
        }
    }
}